package com.blockchain.console.servlets.financial.cz;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.enums.ChargeStatus;
import com.blockchain.common.enums.ChargeType;
import com.blockchain.common.enums.IsPass;
import com.blockchain.console.service.financial.CzglManage;
import com.blockchain.console.service.financial.entity.CzglRecord;
import com.blockchain.console.service.financial.query.CzglRecordQuery;
import com.blockchain.console.servlets.financial.AbstractFinancialServlet;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.query.Paging;
import com.blockchain.framework.service.query.PagingResult;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.util.parser.IntegerParser;
import com.blockchain.util.parser.TimestampParser;

@Right(id = "C_FINANCIAL_EXPORT", name = "用户充值-导出")
public class ExportCzgl extends AbstractFinancialServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request,
			HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		processPost(request, response, serviceSession);
	}

	@Override
	protected void processPost(final HttpServletRequest request,
			final HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		CzglManage recharge = serviceSession.getService(CzglManage.class);
		response.setHeader("Content-disposition", "attachment;filename="
				+ new Timestamp(System.currentTimeMillis()).getTime() + ".csv");
		response.setContentType("application/csv");
		PagingResult<CzglRecord> result = recharge.getUserRechargeRecordList(
				new CzglRecordQuery() {

					@Override
					public ChargeStatus getStatus() {
						return EnumParser.parse(ChargeStatus.class, request.getParameter("status"));
					}

					@Override
					public Timestamp getStartRechargeTime() {
						return TimestampParser.parse(request.getParameter("startRechargeTime"));
					}
					@Override
					public String getLoginName() {
						return request.getParameter("loginName");
					}

					@Override
					public Timestamp getEndRechargeTime() {
						return TimestampParser.parse(request.getParameter("endRechargeTime"));
					}
					@Override
					public String getName() {
						return request.getParameter("name");
					}

					@Override
					public ChargeType getChargeType() {
						return EnumParser.parse(ChargeType.class, request.getParameter("payType"));
					}

					@Override
					public String getYhhk() {
						return request.getParameter("kh");
					}

					@Override
					public IsPass getZddz() {
						return EnumParser.parse(IsPass.class, request.getParameter("is"));
					}

					@Override
					public int getUserid() {
						return IntegerParser.parse(request.getParameter("userid"));
					}

				}, new Paging() {
					@Override
					public int getSize() {
						return Integer.MAX_VALUE;
					}

					@Override
					public int getCurrentPage() {
						return IntegerParser.parse(request.getParameter(PAGING_CURRENT));
					}
				});
		recharge.export(result.getItems(), response.getOutputStream(), "");
	}
}
